<?php
namespace Home\Controller;

class CouponController extends ParentController {
    protected function _initialize() {
        $this->hasRight = parent::isLogin();
         parent::switchTimeUtc();
    }

    /**
     * 用户获取优惠券
     * @modify wenhui 2018-01-08    创建
     */
    public function addUserCoupon() {
        if (empty($this->hasRight)) {
            returnJson(403, 304);
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'couponNo' => 'notnull,notempty,cpOrder',
        );
        try {
            \Org\Util\Validate::validate($param, $validateRule);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        $userId = getUserId();
        // 调用RPC获取信息
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotelId' => $param['hotel'],
            'couponNo' => $param['couponNo'],
            'userId' => $userId,
        );
        $result = $client->addUserCoupons($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            returnJson(200, 200, $result['data']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 获取用户优惠券
     * @modify wenhui 2018-01-08    创建
     */
    public function getUserCoupon() {
        if (empty($this->hasRight)) {
            returnJson(403, 304);
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'type' => 'notnull,notempty,id', // 1=有效 2=历史
        );
        try {
            \Org\Util\Validate::validate($param, $validateRule);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        $userId = getUserId();
        // 调用RPC获取信息
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotelId' => $param['hotel'],
            'type' => $param['type'],
            'userId' => $userId,
            'bookLangSet' => LANG_SET,
        );
        $result = $client->getUserCoupons($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            returnJson(200, 200, $result['data']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 获取用户下单可用优惠券
     * @modify wenhui 2018-01-08    创建
     */
    public function getUserCouponCanBook() {
        // 微信登录 没有绑定手机号 即 USER_ID = 0  算没有登录 需要绑定再领取优惠券
        $isLogin = !is_null(session(C('USER_ID'))) && session(C('USER_ID')) != 0 ? true : false;
        if (!$isLogin) {
            returnJson(200, 200, array());
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'roomTypeId' => 'notnull,notempty,id',
            'price' => 'notnull,notempty,decimal8_2',
            'charityPrice' => 'notnull,notempty,decimal8_2',
        );
        try {
            \Org\Util\Validate::validate($param, $validateRule);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        $userId = getUserId();
        // 调用RPC获取信息
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotelId' => $param['hotel'],
            'type' => 1,
            'userId' => $userId,
            'bookLangSet' => LANG_SET,
        );
        $result = $client->getUserCoupons($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            $userCoupons = $result['data'];
            if (!empty($userCoupons)) {
                // 处理数据
                $roomTypeId = $param['roomTypeId'];
                $price = $param['price'];
                $charityPrice = $param['charityPrice'];
                $currentWeek = date('w');
                $currentDate = date('Y-m-d');
                foreach ($userCoupons as $key => $coupon) {
                    $roomTypeIdArray = array_column($coupon['couponRoomType'], 'roomtype_id');
                    // 是否优惠券房型
                    if (!in_array($roomTypeId, $roomTypeIdArray)) {
                        unset($userCoupons[$key]);
                        continue;
                    }

                    // 过滤不可用星期
                    if (in_array($currentWeek, $coupon['couponWeek'])) {
                        unset($userCoupons[$key]);
                        continue;
                    }

                    $couponDateArray = array_column($coupon['couponDate'], 'date');
                    // 过滤不可用日期
                    if (in_array($currentDate, $couponDateArray)) {
                        unset($userCoupons[$key]);
                        continue;
                    }

                    // 是否瞒住优惠金额
                    if (($price-$charityPrice) < $coupon['couponPrice']) {
                        unset($userCoupons[$key]);
                        continue;
                    }

                    // 过滤不在有效期的优惠券
                    $currentDate = date('Y-m-d');
                    if (!isBetweenTime($currentDate, $coupon['couponStart'], $coupon['couponEnd'])) {
                        unset($userCoupons[$key]);
                        continue;
                    }
                }
                unset($item);
            }

            $userCoupons = array_values($userCoupons);
            returnJson(200, 200, $userCoupons);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }
}
